package logic

import (
	"context"
	"errors"
	"srv/model/mysql"
	"srv/model/redis"
	"srv/pkg"

	"srv/internal/svc"
	"srv/renrenzu"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *renrenzu.LoginRequest) (*renrenzu.LoginResponse, error) {
	// todo: add your logic here and delete this line
	redis.Rdb.Get(context.Background(), "sendSmslogin"+in.Phone)
	u := mysql.Users{}
	err := u.Where(in.Phone)
	if err != nil {
		logx.Error("用户查询失败：", err)
		return nil, errors.New("用户查询失败")
	}
	if u.ID == 0 {
		logx.Error("用户不存在：", err)
		return nil, errors.New("用户不存在")
	}
	if u.Password != pkg.Md5(in.Password) {
		logx.Error("密码错误")
		return nil, errors.New("密码错误")
	}
	//redis.Rdb.Del(context.Background(),"sendSmslogin"+in.Phone)
	return &renrenzu.LoginResponse{UserId: int64(u.ID)}, nil
}
